package com.TeacherInfoServer.controller;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.listener.PostDoctoralListener;
import com.TeacherInfoServer.pojo.DTO.AddPostDoctoralDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.PostDoctoralSearchDTO;
import com.TeacherInfoServer.pojo.DTO.UpdatePostDoctoralDTO;
import com.TeacherInfoServer.pojo.excelImport.PostDoctoralExcel;
import com.TeacherInfoServer.pojo.vo.PostDoctoralListVo;
import com.TeacherInfoServer.common.service.PostDoctoralService;
import com.alibaba.excel.EasyExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/s1/post_doctoral")
@Tag(name = "博士后信息控制器",description = "用于对博士后信息的控制")
public class PostDoctoralController {

    @Autowired
    private PostDoctoralService postDoctoralService;

    @Autowired
    private PostDoctoralListener postDoctoralListener;

    @PostMapping("/addOne")
    @Operation(summary = "添加单个博士后信息",description = "添加单个博士后信息")
    public Result<Map<String,Integer>> addOne(@RequestBody @Valid AddPostDoctoralDTO addPostDoctoralDTO){
        return postDoctoralService.addOne(addPostDoctoralDTO);
    }

    @PostMapping("/addByBatch")
    @Operation(summary = "批量添加博士后信息",description = "通过excel表导入博士后信息")
    public Result<Map<String,Integer>> addByBatch(@RequestParam("file") MultipartFile file) throws IOException {
        postDoctoralListener.initList();
        EasyExcel.read(file.getInputStream(), PostDoctoralExcel.class,postDoctoralListener).sheet().doRead();
        Map<String,Integer> map = new HashMap<>();
        map.put("record",postDoctoralListener.getRecord());
        return Result.success(map);
    }

    @GetMapping(value = "/getList",params = {"pageNumber","pageSize"})
    @Operation(summary = "获取博士后信息列表",description = "分页博士后信息列表")
    public Result<PostDoctoralListVo> getList(@RequestParam Long pageNumber,@RequestParam Long pageSize){
        return postDoctoralService.getList(pageNumber,pageSize);
    }
    @Operation(summary = "模糊查询获取博士后信息列表",description = "模糊查询分页获取博士后信息列表")
    @PostMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestBody PostDoctoralSearchDTO postDoctoralSearchDTO){
        return Result.success("获取教师学习经历列表成功",postDoctoralService.searchPostDoctoralList(postDoctoralSearchDTO));
    }

    @GetMapping(value = "/export",params = "fileName")
    public void export(HttpServletResponse response, @RequestParam String fileName) throws IOException{
        postDoctoralService.export(response,fileName);
    }

    @DeleteMapping(value = "/delete",params = "teacherIds")
    @Operation(summary = "批量删除博士后信息",description = "根据工号列表批量删除博士后信息")
    public Result<Map<String,Integer>> deleteBatch(@RequestParam String[] teacherIds){
        return postDoctoralService.deleteBatch(teacherIds);
    }

    @PutMapping(value = "/update")
    @Operation(summary = "更新单个博士后信息",description = "更新单个博士后信息")
    public Result<Map<String,Integer>> updateOne(@RequestBody @Valid UpdatePostDoctoralDTO updatePostDoctoralDTO){
        return postDoctoralService.updateOne(updatePostDoctoralDTO);
    }
}
